﻿#include <wx/wx.h>
#include "component/MyFrame.h"
#include "diagram/DiagFrame.h"
#include <windows.h>





class MyApp : public wxApp
{
public:
    virtual bool OnInit() {
        if (!wxImage::FindHandler(wxBITMAP_TYPE_PNG)) {
            wxImage::AddHandler(new wxPNGHandler);
        }
        DiagFrame* diagframe = new DiagFrame(wxT("智绘EDA"));
        wxString imagePath = wxT("logo.png");
        wxImage appImage(imagePath, wxBITMAP_TYPE_PNG);
        if (!appImage.IsOk()) {
            wxMessageBox(wxT("无法打开文件: ") + imagePath);
            return false; // 加载失败，返回 false
        }
        appImage.Rescale(100, 100);
        wxBitmap appBitmap(appImage);
        wxIcon appIcon;
        appIcon.CopyFromBitmap(appBitmap);
        diagframe->SetIcon(appIcon);
        diagframe->Show(true);

        return true; // 加载成功，返回 true
    }
};

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void CreateNewWindow();

// 全局变量，用于存储背景图像句柄
HBITMAP g_hBackgroundBitmap = NULL;
// 全局变量，用于存储新窗口的句柄
HWND g_hNewWindow = NULL;
HINSTANCE hInstance1;
HINSTANCE hPrevInstance1;
wxCmdLineArgType lpCmdLine1;
int nCmdShow1;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    hInstance1 = hInstance;
    hPrevInstance1 = hPrevInstance;
    lpCmdLine1 = lpCmdLine;
    nCmdShow1 = nCmdShow;
    // Register the window class.
    const wchar_t CLASS_NAME[] = L"CustomWindowClass";

    WNDCLASS wc = { };
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hbrBackground = NULL; // 设置背景为NULL，将由后面的代码设置背景

    RegisterClass(&wc);

    // Create the window.
    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Custom Window Background",    // Window text
        WS_POPUP,            // Window style

        // Size and position
        450,  // 初始窗口左上角 x 坐标
        250,  // 初始窗口左上角 y 坐标
        835,  // 窗口宽度 712.5
        703,  // 窗口高度

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );

    if (hwnd == NULL) {
        return 0;
    }

    // Load the background image
    g_hBackgroundBitmap = (HBITMAP)LoadImage(NULL, L"欢迎界面.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    if (g_hBackgroundBitmap != NULL) {
        // Show the window to calculate its client area size
        ShowWindow(hwnd, nCmdShow);
    }
    else {
        // Display an error message box if image loading failed
        MessageBox(NULL, L"Failed to load background image!", L"Error", MB_ICONERROR | MB_OK);
    }

    // Create a button
    HWND hButton = CreateWindowEx(
        0,                      // Optional window styles.
        L"BUTTON",              // Button class
        L"开始使用",            // Button text
        WS_VISIBLE | WS_CHILD | BS_FLAT | BS_BITMAP,  // Styles
        318.5,                     // x position
        405,                     // y position
        100,                    // Button width
        40,                     // Button height
        hwnd,                   // Parent window
        (HMENU)1,               // Button ID
        hInstance,              // Instance handle
        NULL                    // Additional application data
    );
    // 加载位图
    HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, L"开始.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    // 将位图设置到按钮上
    SendMessage(hButton, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);

    // Run the message loop.
    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_DESTROY:
        // Cleanup resources before exiting
        if (g_hBackgroundBitmap != NULL) {
            DeleteObject(g_hBackgroundBitmap);
        }
        PostQuitMessage(0);
        return 0;

    case WM_PAINT: {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        if (g_hBackgroundBitmap != NULL) {
            // Get the size of the client area
            RECT rect;
            GetClientRect(hwnd, &rect);
            int width = rect.right - rect.left;
            int height = rect.bottom - rect.top;

            // Create a memory device context and select the background bitmap into it
            HDC hdcMem = CreateCompatibleDC(hdc);
            HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, g_hBackgroundBitmap);

            // Stretch draw the background bitmap to fit the client area
            StretchBlt(hdc, 0, 0, width, height, hdcMem, 0, 0,
                962, 768, SRCCOPY);

            // Cleanup
            SelectObject(hdcMem, hOldBitmap);
            DeleteDC(hdcMem);
        }

        EndPaint(hwnd, &ps);
        break;
    }

    case WM_SIZE: {
        // Redraw the window when its size changes
        InvalidateRect(hwnd, NULL, TRUE);
        break;
    }

    case WM_COMMAND: {
        // Handle button click event
        if (LOWORD(wParam) == 1) {
            // 创建新窗口
            MyApp* app = new MyApp();
            wxApp::SetInstance(app);
            wxEntry(hInstance1, hPrevInstance1, lpCmdLine1, nCmdShow1);
            // 最小化当前窗口
            ShowWindow(hwnd, SW_MINIMIZE);
        }
        break;
    }
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

//IMPLEMENT_APP(MyApp)
